package it.agm.fanta.adapter;

import it.agm.fanta.FantaserviceApp;
import it.agm.fanta.R;
import it.agm.fanta.model.Giocatore;
import it.agm.fanta.model.GiocatorePartitaLega;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class GiocatoriPartitaLegaAdapter extends ArrayAdapter<GiocatorePartitaLega> {
	
	private static final char CHAR_ZERO = '0';

	public GiocatoriPartitaLegaAdapter(Context context, int textViewResourceId,
			ArrayList<GiocatorePartitaLega> objects) {
        super(context, textViewResourceId, objects);
    }

	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_calciatore_partita_lega, null);
        LinearLayout linearLayoutRoot = (LinearLayout) convertView.findViewById(R.id.tabgiocatorepartitalega);
        TextView nomeGiocatore = (TextView)linearLayoutRoot.findViewById(R.id.nomeGiocatorePartitaLega);
        TextView ruolo = (TextView)linearLayoutRoot.findViewById(R.id.ruolo);
        TextView voto = (TextView)linearLayoutRoot.findViewById(R.id.voto);
        LinearLayout linearLayout = (LinearLayout) linearLayoutRoot.findViewById(R.id.bonusmalus);
        
        GiocatorePartitaLega giocatore = getItem(position);
	    if('Q' == giocatore.getLineup()){
	    	linearLayoutRoot.setBackgroundColor(FantaserviceApp.getAppContext().getResources().getColor(R.color.black_overlay));
	    	nomeGiocatore.setTextColor(FantaserviceApp.getAppContext().getResources().getColor(R.color.white));
	    	voto.setVisibility(View.GONE);
	    }
        nomeGiocatore.setText(giocatore.getNome() + " " + giocatore.getCognome());
        ruolo.setText("" + giocatore.getRuolo());
        if( giocatore.getPunteggio() >= 0){
        	voto.setText("" + giocatore.getPunteggio());
        }else{
        	voto.setText("-");
        }
        if(giocatore.isSostituito()){
        	Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.uscita);
        	ImageView image = new ImageView(convertView.getContext());
			image.setImageDrawable(drawable);
			linearLayout.addView(image);
			
        }
        if(Giocatore.PORTIERE == giocatore.getRuolo() && "00000000000".equals(giocatore.getBonusmalus()) && giocatore.getPunteggio() > -1){
        	Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.porta_imbattuta);
			ImageView image = new ImageView(convertView.getContext());
			image.setImageDrawable(drawable);
			linearLayout.addView(image);
		}else{
			
			if(giocatore.isIngresso()){
				Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.ingresso);
	        	ImageView image = new ImageView(convertView.getContext());
				image.setImageDrawable(drawable);
				linearLayout.addView(image);
			}
			
			char[] bonusMalus = giocatore.getBonusmalusCharArray();
	        for (int i = 0; i < bonusMalus.length; i++) {
	        	int valore = Character.getNumericValue(bonusMalus[i]);
	        	boolean golVittoria = Character.getNumericValue(bonusMalus[9])>0;
	        	boolean golPareggio = Character.getNumericValue(bonusMalus[10])>0;
				switch (i) {
				case 0:
					if(bonusMalus[i]>CHAR_ZERO){
						//int valore = Character.getNumericValue(bonusMalus[i]);
						for(int j = 1; j <= valore; j++){
							int idImage = R.drawable.gol_segnato;
							if(golVittoria && j==valore){
								idImage = R.drawable.gol_vittoria;
							}else if(golPareggio && j==valore){
								idImage = R.drawable.gol_pareggio;
							}
							Drawable drawable = convertView.getContext().getResources().getDrawable(idImage);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
							linearLayout.addView(image);
						}
					}
					break;
				case 1:
					if(bonusMalus[i]>CHAR_ZERO){
						//Integer valore = new Integer(bonusMalus[i]);
						for(int j = 1; j <= valore; j++){
							Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.gol_subito);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
	//						image.setScaleType(ScaleType.FIT_);
							linearLayout.addView(image);
						}
					}
					break;
				case 2:
					if(bonusMalus[i]>CHAR_ZERO){
						//Integer valore = new Integer(bonusMalus[i]);
						for(int j = 1; j <= valore; j++){
							Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.autorete);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
	//						image.setScaleType(ScaleType.FIT_);
							linearLayout.addView(image);
						}
					}
					break;	
				case 3:
					if(bonusMalus[i]>CHAR_ZERO){
						//Integer valore = new Integer(bonusMalus[i]);
						for(int j = 1; j <= valore; j++){
							Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.assist);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
	//						image.setScaleType(ScaleType.FIT_);
							linearLayout.addView(image);
						}
					}
					break;	
				case 4:
					if(bonusMalus[i]>CHAR_ZERO){
						//Integer valore = new Integer(bonusMalus[i]);
						for(int j = 1; j <= valore; j++){
							Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.ammonizione);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
	//						image.setScaleType(ScaleType.FIT_);
							linearLayout.addView(image);
						}
					}
					break;
				case 5:
					if(bonusMalus[i]>CHAR_ZERO){
						//Integer valore = new Integer(bonusMalus[i]);
						for(int j = 1; j <= valore; j++){
							Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.espulsione);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
	//						image.setScaleType(ScaleType.FIT_);
							linearLayout.addView(image);
						}
					}
					break;
				case 6:
					if(bonusMalus[i]>CHAR_ZERO){
						//Integer valore = new Integer(bonusMalus[i]);
						for(int j = 1; j <= valore; j++){
							Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.rigore_sbagliato);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
	//						image.setScaleType(ScaleType.FIT_);
							linearLayout.addView(image);
						}
					}
					break;
				case 7:
					if(bonusMalus[i]>CHAR_ZERO){
						for(int j = 1; j <= valore; j++){
							Drawable drawable = convertView.getContext().getResources().getDrawable(R.drawable.rigore_parato);
							
							ImageView image = new ImageView(convertView.getContext());
							image.setImageDrawable(drawable);
							linearLayout.addView(image);
						}
					}
					break;	
				default:
					break;
				}
	        }
		}
        return convertView;
    }

	
}
